package com.cgx.tablet.beeper.api;

import android.text.TextUtils;

import com.cgx.tablet.beeper.MyApplication;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * SUV-android API工具类
 * Created by Droven on 2017/4/13.
 */

public class ApiUtil {

    public static ApiService apiService;
    public static String session = "";
    public static long startTime = 0;

    public static void init(String baseUrl) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)// 连接超时
                .writeTimeout(30, TimeUnit.SECONDS)// 上传超时
                .addInterceptor(new CustomInterceptor())//拦截器
                .addInterceptor(new ReceivedCookiesInterceptor(MyApplication.getGlobalContext()))
                .addInterceptor(new AddCookiesInterceptor(MyApplication.getGlobalContext()))
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        apiService = retrofit.create(ApiService.class);
    }

    /**
     * 转码
     */
    public static String encodeString(String key) {
        if (TextUtils.isEmpty(key)) return "";
        String newKey = "";
        try {
            newKey = URLEncoder.encode(key, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return newKey;
    }

}
